package cn.edu.ldh.toolapp.slice;

import cn.edu.ldh.toolapp.ResourceTable;
import cn.edu.ldh.toolapp.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.ai.cv.common.ConnectionCallback;
import ohos.ai.cv.common.VisionManager;
import ohos.ai.cv.qrcode.IBarcodeDetector;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import java.io.*;

public class CodeImgAbilitySlice extends AbilitySlice {

    private final static HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 1, "CodeImgAbilitySlice");
    private IBarcodeDetector iBarcodeDetector;
    private TextField txtTf;
    private Image codeImg;
    private Text tipTxt;
    private PixelMap pixelMap;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_codeImg);
        this.startInit();
    }

    private void startInit() {
        txtTf = (TextField) findComponentById(ResourceTable.Id_code_txt_tf);
        codeImg = (Image) findComponentById(ResourceTable.Id_code_codeImg_img);
        tipTxt = (Text) findComponentById(ResourceTable.Id_code_tip_txt);
        findComponentById(ResourceTable.Id_code_sub_btn).setClickedListener(this::setCodeImg);
        findComponentById(ResourceTable.Id_code_back_btn).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
        VisionManager.init(this, connectionCallback);
    }

    //生成二维码并显示
    private void setCodeImg(Component component) {
        if(txtTf.getText().equals("")) {
            ToastUtil.showToast(this, "不能为空");
            return;
        }
        iBarcodeDetector = VisionManager.getBarcodeDetector(getAbility());
        final int IMAGE_LENGTH = 512;
        byte[] byteArray = new byte[IMAGE_LENGTH * IMAGE_LENGTH * 4];
        int result = iBarcodeDetector
                .detect(txtTf.getText(), byteArray, IMAGE_LENGTH, IMAGE_LENGTH);
        if(result == 0) {
            txtTf.clearFocus();
            InputStream inputStream = new ByteArrayInputStream(byteArray);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/jpeg";
            ImageSource imageSource = ImageSource.create(inputStream, sourceOptions);
            pixelMap = imageSource.createPixelmap(null);
            codeImg.setPixelMap(pixelMap);
            iBarcodeDetector.release();
            txtTf.setText("");
            ToastUtil.showToast(this, "生成成功");
        } else {
            ToastUtil.showToast(this, "生成失败");
        }
    }

    private final ConnectionCallback connectionCallback = new ConnectionCallback() {
        @Override
        public void onServiceConnect() {
            HiLog.error(LABEL, "链接生成成功");
        }

        @Override
        public void onServiceDisconnect() {

        }
    };

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(iBarcodeDetector != null){
            iBarcodeDetector.release();
        }
        VisionManager.destroy();
    }
}
